# -*- coding : utf-8 -*-

def index():
    if session.user_name:
        message = '欢迎 ' + str(session.user_name) + '！！！'
    elif request.vars.keys() == req_keys_list['alisoft']:
        session.vars = dict(request.vars)
        vars = session.vars
        vars['appId'] = vars.pop('app_id')
        vars['userId'] = vars.pop('user_id')
        vars['appInstanceId'] = vars.pop('app_instance_id')
        del vars['target']
        session.sip_dict = taobao_api.sip_dict_all(vars)
        redirect(URL(r=request, c='start', f='index'))
    else:
        message = None

    if request.vars.keys() == req_keys_list['login']:
        session.sip_sessionid = request.vars['isp_username']

    return dict(
        message = message
        )

def logout():
    session.user_name = False
    redirect(URL(r=request, f='index'))

def login():
    session.user_name = True
    redirect(URL(r=request, f='index'))

def fast_login():
    session.user_name = 'fast_login'
    redirect(URL(r=request, f='index'))

def re_login():
    if session.user_name == False:
        redirect(MY_ALISOFT_URL)
    else:
        session.user_name = False
        redirect(URL(r=request, f='re_login'))

def clear_session():
    del_ok = ''
    message = '不需要清除session'
    try:
        del_ok = request.args[0]
    except:
        pass

    x = ['session.sip_dict', 'session.vars', 'session.user_name', 'session.sip_sessionid']
    y = []
    for i in range(len(x)):
        if eval(x[i]):
            y.append(x[i])
    for i in y:
        if i in x:
            message = None
        
    if del_ok == 'yes':
        for i in y:
            exec('del %s') % i
        redirect(E)
    elif del_ok == 'no':
        redirect(E)

    return dict(
        del_ok = del_ok,
        message = message,
        )

